package com.cheng.example.juc;

import lombok.Data;

public class TestVolatile {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo td=new ThreadDemo();
        new Thread(td).start();

        for (;;) {
//            td.inf = td.getInf() + 1;
//            Thread.sleep(100);
//            td.inf = td.getInf() - 1;
            System.out.println(td.getInf());
            if(td.getInf()==1){
                System.out.println("-----------"+td.getInf());
                break;
            }
        }
    }

}

@Data
class ThreadDemo implements Runnable{
    private boolean flag=false;

    public int inf=0;
    public void run() {
        try {
            Thread.sleep(201);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag=true;
        inf = 1;
//        System.out.println("flag="+isFlag());
    }
    public boolean isFlag(){
        return flag;
    }
    public void setFlag(boolean flag){
        this.flag=flag;
    }
}
